package tech.runchen.gz5055.oms.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import tech.runchen.gz5055.oms.common.annotation.RequiresPermissions;
import tech.runchen.gz5055.oms.common.annotation.SysLog;
import tech.runchen.gz5055.oms.common.enums.BusinessType;
import tech.runchen.gz5055.oms.common.enums.UserType;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.common.utils.SecurityUtils;
import tech.runchen.gz5055.oms.common.validator.Assert;
import tech.runchen.gz5055.oms.common.base.AbstractController;
import tech.runchen.gz5055.oms.modules.system.entity.UserEntity;
import tech.runchen.gz5055.oms.modules.system.form.PasswordForm;
import tech.runchen.gz5055.oms.modules.system.form.PsCharForm;
import tech.runchen.gz5055.oms.modules.system.service.IUserService;

import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-02-14
 */
@Api(tags = "管理平台 - 用户信息")
@RestController
@RequestMapping("/sys/user")
public class SysUserController extends AbstractController {

    @Autowired
    private IUserService userService;

    @ApiOperation("用户信息列表")
    @RequiresPermissions()
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageInfo page = userService.queryPage(params);
        return Result.success(page);
    }

    @ApiOperation("当前登录用户信息")
    @GetMapping("/info")
    public Result info() {
        UserEntity user = userService.getById(getLoginUser().getUser().getId());
        return Result.success(user);
    }

    @ApiOperation("用户详情信息")
    @RequiresPermissions()
    @GetMapping("/{userId}")
    public Result info(@PathVariable("userId") Long userId) {
        UserEntity user = userService.getById(userId);
        return Result.success(user);
    }

    @SysLog(title = "保存用户信息", businessType = BusinessType.INSERT)
    @ApiOperation("保存用户信息")
    @RequiresPermissions()
    @PostMapping("/save")
    public Result save(@Validated @RequestBody UserEntity user) {
        user.setCreateBy(user.getUserName());
        userService.saveUser(user);
        return Result.success();
    }

    @SysLog(title = "更新用户信息", businessType = BusinessType.UPDATE)
    @ApiOperation("更新用户信息")
    @RequiresPermissions()
    @PostMapping("/update")
    public Result update(@Validated @RequestBody UserEntity user) {
        user.setUpdateBy(user.getUserName());
        userService.updateUser(user);
        return Result.success();
    }

    @SysLog(title = "删除用户信息", businessType = BusinessType.DELETE)
    @ApiOperation("删除用户信息")
    @RequiresPermissions()
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] userIds) {
        return toResult(userService.deleteBatch(userIds));
    }

    @SysLog(title = "修改用户密码", businessType = BusinessType.OTHER)
    @ApiOperation("修改密码")
    @RequiresPermissions()
    @PostMapping("/password")
    public Result password(@RequestBody PasswordForm form) {
        Assert.isBlank(form.getNewPassword(), "新密码不为能空");

        //md5加密
        String password = SecurityUtils.encryptPassword(form.getPassword());
        //md5加密
        String newPassword = SecurityUtils.encryptPassword(form.getNewPassword());

        //更新密码
        return toResult(userService.updatePassword(getLoginUser().getUser().getId(), password, newPassword), "原密码不正确");
    }

    @SysLog(title = "修改用户指纹", businessType = BusinessType.OTHER)
    @ApiOperation("修改用户指纹")
    @RequiresPermissions()
    @PostMapping("/psChar")
    public Result psChar(@RequestBody PsCharForm form) {
        Assert.isBlank(form.getPsChar(), "指纹不为能空");
        //更新指纹
        return toResult(userService.updatePsChar(getLoginUser().getUser().getId(), form.getPsChar()), "指纹特征更新失败");
    }
}
